package com.huilian.iotbox.client.service.impl;

import com.huilian.iotbox.client.dao.CommodityStoreUpDownDao;
import com.huilian.iotbox.client.service.CommodityStoreUpDownService;
import com.huilian.iotbox.client.service.DeviceService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.CommodityStoreUpDownDto;
import com.huilian.iotbox.data.enums.*;
import com.huilian.iotbox.data.po.CommodityProjectUpDownDatabase;
import com.huilian.iotbox.data.po.CommodityStoreUpDown;
import com.huilian.iotbox.data.po.SystemConfig;
import com.huilian.iotbox.data.service.SystemConfigService;
import com.huilian.iotbox.data.utils.MD5SignUtil;
import com.huilian.iotbox.data.vo.CommodityStoreUpDownVo;
import com.huilian.iotbox.data.vo.CommodityVo;
import com.huilian.iotbox.data.vo.DeviceServicePackageVo;
import com.huilian.iotbox.data.vo.DeviceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LinYinDong
 * @date 2023/5/31 20:04
 */
@Service
@Slf4j
public class CommodityStoreUpDownServiceImpl implements CommodityStoreUpDownService {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private SystemConfigService systemConfigService;
    @Resource
    private CommodityStoreUpDownDao commodityStoreUpDownDao;

    @Override
    public CommodityStoreUpDownVo queryById(Long id) {
        return commodityStoreUpDownDao.queryById(id);
    }

    @Override
    public int update(CommodityStoreUpDownDto commodityStoreUpDownDto) {
        return commodityStoreUpDownDao.update(commodityStoreUpDownDto);
    }

    @Override
    public CommodityVo findOneCommodityByStoreUpDownId(Long id) {
        return commodityStoreUpDownDao.findOneCommodityByStoreUpDownId(id);
    }


    @Override
    public CommodityStoreUpDownVo findByCommodity(CommodityStoreUpDownDto commodityStoreUpDownDto) {
        return commodityStoreUpDownDao.findByCommodity(commodityStoreUpDownDto);
    }

    @Override
    public CommodityStoreUpDown findOneCommodityStoreUpDownByCondition(CommodityStoreUpDown commodityStoreUpDownCondition) {
        return commodityStoreUpDownDao.findOneCommodityStoreUpDownByCondition(commodityStoreUpDownCondition);
    }

    @Override
    public ServerResponse getAllCommodityByAtt(String deviceNo, String att) {
        log.info("安卓扫码传入商品参数：{}", att);
        HashMap<Long, Integer> keyValueMap = new HashMap<>();
        List<Long> idList = new ArrayList<>();
        String[] pairs = att.split("\\^");
        for (String pair : pairs) {
            String[] parts = pair.split("_");
            if (parts.length == 2) {
                Long key = Long.valueOf(parts[0]);
                int value = Integer.parseInt(parts[1]);
                keyValueMap.put(key, value);
                idList.add(key);
            }
        }
        List<CommodityStoreUpDownVo> list = commodityStoreUpDownDao.getAllCommodityByAtt(deviceNo, idList);
        for (CommodityStoreUpDownVo commodityStoreUpDownVo : list) {
            for (Map.Entry<Long, Integer> entry : keyValueMap.entrySet()) {
                Long key = entry.getKey();
                Integer value = entry.getValue();
                if (commodityStoreUpDownVo.getCommodityStoreUpDownId().equals(key)) {
                    commodityStoreUpDownVo.setNum(value);
                    if (ObjectUtils.isEmpty(commodityStoreUpDownVo.getAlias())) {
                        String alias = convertToShelfNum(commodityStoreUpDownVo.getHierarchy(), commodityStoreUpDownVo.getFreightLane());
                        commodityStoreUpDownVo.setAlias(alias);
                    }
                }
            }
        }
        return ServerResponse.createBySuccess(list);
    }


    public static String convertToShelfNum(Integer hierarchy, Integer freightLane) {
        return String.valueOf(hierarchy * 100 + freightLane);
    }
}
